send = true;

function loginOnEnter(event) {
	if (event.keyCode == 13)
		login();
}

function login() {
	if (send) {
		document.getElementById('login_message').className = "";
		document.getElementById('login_message').innerHTML = "Проверка...";
		send = false;
		var data1 = document.getElementById('j_username').value;
		var data2 = document.getElementById('j_password').value;
		document.getElementById('j_username').disabled = true;
		document.getElementById('j_password').disabled = true;
		document.getElementById('login_enter').disabled = true;

		ajaxSendPOST('/one/j_spring_security_check', encodeURI("j_username=" + data1
				+ "&j_password=" + data2), SendCallback);
	}
}
function SendCallback(status, statusText, answer) {
	// alert(status + " " + answer);
	if (status == 200 || status == 304 || status == 404 ){
//		document.getElementById('login_message').className = "";
//		document.getElementById('login_message').innerHTML = "";
		location.reload(true);
	}
	else if (status == 401) {
		document.getElementById('login_message').className = "error";
		if (answer.indexOf("Authentication Failed: Maximum sessions of 1 for this principal exceeded") !=-1)
			document.getElementById('login_message').innerHTML = "Такой пользователь уже в системе";
		else if(answer.indexOf("Authentication Failed: Bad credentials") != -1)
			document.getElementById('login_message').innerHTML = "Неверный логин или пароль";
		else
			document.getElementById('login_message').innerHTML = statusText;
			
	} else {
		document.getElementById('login_message').className = "error";
		document.getElementById('login_message').innerHTML = "Сервер недоступен";
	}
	send = true;
	document.getElementById('j_username').value = "";
	document.getElementById('j_password').value = "";
	document.getElementById('j_username').disabled = false;
	document.getElementById('j_password').disabled = false;
	document.getElementById('login_enter').disabled = false;
}

function ajaxSendPOST(xmlpage, data, callback) {
	var xmlh = null;
	if (window.XMLHttpRequest)
		xmlh = new XMLHttpRequest();
	else
		try {
			xmlh = new ActiveXObject('Msxml2.XMLHTTP');
		} catch (ex) {
			xmlh = new ActiveXObject('Microsoft.XMLHTTP');
		}
	if (xmlh) {
		xmlh.open('post', xmlpage, true);
		xmlh.onreadystatechange = function(x) {
			if (xmlh.readyState == 4) {
				callback(xmlh.status, xmlh.statusText, xmlh.responseText);
			}
		};
		xmlh.setRequestHeader("Accept-Charset", "windows-1251");
		xmlh.setRequestHeader("Accept-Language", "ru, en");
		xmlh.setRequestHeader("Connection", "close");
		xmlh.setRequestHeader("Content-length", data.length);
		// Длинна отправляемых данных
		xmlh.setRequestHeader("Content-type",
				"application/x-www-form-urlencoded");
		xmlh.send(data); // Именно здесь отправляются данные
	}
}